package com.len.controller;

import com.len.base.BaseController;
import com.len.core.annotation.Log;
import com.len.entity.BehaviorField;
import com.len.entity.ReportConfig;
import com.len.exception.MyException;
import com.len.service.BehaviorFieldService;
import com.len.service.ReportConfigService;
import com.len.util.BeanUtil;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author zhuxiaomeng
 * @date 2017/12/19.
 * @email 154040976@qq.com
 * 角色业务
 */
@Controller
@RequestMapping(value = "/reportConfig")
public class ReportConfigController extends BaseController {

    @Autowired
    private ReportConfigService reportConfigService;
    @Autowired
    private BehaviorFieldService behaviorFieldService;

    @GetMapping(value = "showReportConfig")
    @RequiresPermissions(value = "reportConfig:show")
    public String showReportConfig(Model model) {
        return "reportConfig/reportConfigList";
    }

    @ApiOperation(value = "/showReportConfigList", httpMethod = "GET", notes = "展示行为")
    @GetMapping(value = "showReportConfigList")
    @ResponseBody
    @RequiresPermissions("reportConfig:show")
    public ReType showReportConfigList(ReportConfig reportConfig, Model model, String page, String limit) {
        return reportConfigService.show(reportConfig, Integer.valueOf(page), Integer.valueOf(limit));
    }


    @GetMapping(value = "showAddReportConfig")
    public String showAddReportConfig(Model model) {
        return "reportConfig/add-reportConfig";
    }

    /**
     * 根据字典类型获取字典数据
     * @param behaviorField
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "/loadsBehaviorFieldByFlag", httpMethod = "POST", notes = "字典数据")
    @PostMapping(value = "loadsBehaviorFieldByFlag")
    @ResponseBody
    public ReType loadsBehaviorFieldByFlag(BehaviorField behaviorField) throws Exception {
        return behaviorFieldService.loadsBehaviorFieldByFlag(behaviorField);
    }

    @ApiOperation(value = "/addReportConfig", httpMethod = "POST", notes = "添加行为")
    @Log(desc = "添加报表配置")
    @PostMapping(value = "addReportConfig")
    @ResponseBody
    public JsonUtil addReportConfig(ReportConfig reportConfig) {
        if (StringUtils.isEmpty(reportConfig.getReportName())) {
            JsonUtil.error("报表名称不能为空");
        }
        JsonUtil j = new JsonUtil();
        try {
            reportConfig.setCreateDate(new Date());
            reportConfig.setUpdateDate(new Date());
            reportConfigService.insertSelective(reportConfig);
            j.setMsg("保存成功");
        } catch (MyException e) {
            j.setMsg("保存失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

    @GetMapping(value = "updateReportConfig")
    public String updateReportConfig(String id, Model model, boolean detail) {
        if (StringUtils.isNotEmpty(id)) {
            ReportConfig reportConfig = reportConfigService.selectByPrimaryKey(id);
            model.addAttribute("reportConfig", reportConfig);
        }
        model.addAttribute("detail", detail);
        return "reportConfig/update-reportConfig";
    }

    @ApiOperation(value = "/getReportConfigById", httpMethod = "POST", notes = "通过id获取报表配置信息")
    @PostMapping(value = "getReportConfigById")
    @ResponseBody
    public JsonUtil getReportConfigById(String id) {

        JsonUtil j = new JsonUtil();
        if (StringUtils.isNotEmpty(id)) {
            try {
                ReportConfig reportConfig = reportConfigService.selectByPrimaryKey(id);
                j.setMsg("保存成功");
                j.setData(reportConfig);
            } catch (MyException e) {
                j.setMsg("保存失败");
                j.setFlag(false);
                e.printStackTrace();
            }
        }
        return j;
    }
//
    @ApiOperation(value = "/updateReportConfig", httpMethod = "POST", notes = "更新行为")
    @Log(desc = "更新报表配置")
    @PostMapping(value = "updateReportConfig")
    @ResponseBody
    public JsonUtil updateReportConfig(ReportConfig reportConfig) {
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setFlag(false);
        if (reportConfig == null) {
            jsonUtil.setMsg("获取数据失败");
            return jsonUtil;
        }
        try {
            ReportConfig oldReportConfig = reportConfigService.selectByPrimaryKey(reportConfig.getId());
            BeanUtil.copyNotNullBean(reportConfig, oldReportConfig);
            oldReportConfig.setUpdateDate(new Date());
            reportConfigService.updateByPrimaryKeySelective(oldReportConfig);

            jsonUtil.setFlag(true);
            jsonUtil.setMsg("修改成功");
        } catch (MyException e) {
            jsonUtil.setMsg("修改失败");
            e.printStackTrace();
        }
        return jsonUtil;
    }

    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除报表配置")
    @Log(desc = "删除报表配置", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "del")
    @ResponseBody
    @RequiresPermissions("reportConfig:del")
    public JsonUtil del(String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonUtil.error("获取数据失败");
        }
        ReportConfig reportConfig = new ReportConfig();
        reportConfig.setId(id);
        JsonUtil j = new JsonUtil();
        try {
            reportConfigService.deleteByPrimaryKey(id);
            j.setMsg("删除成功");
        } catch (MyException e) {
            j.setMsg("删除失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

}
